package com.fhz.a2102shopjava.util;

import com.fhz.a2102shopjava.callback.LiveDataCallAdapterFactory;
import com.fhz.a2102shopjava.cons.Cons;
import com.fhz.a2102shopjava.interceptor.TokenInterceptor;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 时间:2023/8/14
 *
 * @author Mr.Feng
 * 简述: 工具类
 * 懒汉模式
 */
public class RetrofitUtil {
    private volatile static RetrofitUtil retrofitUtil = null;

    private Retrofit mRetrofit;
    private Retrofit noCallAdapterRetrofit;

    public static RetrofitUtil getInstance() {
        if(retrofitUtil == null){
            synchronized (RetrofitUtil.class){
                if(retrofitUtil == null){
                    retrofitUtil = new RetrofitUtil();
                }
            }
        }
        return retrofitUtil;
    }

    public Retrofit getLiveDataCallAdapterRetrofit(){
        if(mRetrofit == null){
             mRetrofit = new Retrofit.Builder()
                     .baseUrl(Cons.BASE_URL)
                     .client(new OkHttpClient().newBuilder()
                             .addInterceptor(getInterceptor())
                             .addInterceptor(new TokenInterceptor())
                             .build())
                     .addConverterFactory(GsonConverterFactory.create())
                     .addCallAdapterFactory(new LiveDataCallAdapterFactory())
                     .build();
        }
        return mRetrofit;
    }

    /**
     * 1, lunix 目录结构
     * 2, shell 命令 - 脚本.
     * 3, 用户组,用户权限
     * 4, lunix 系统常用命令. cd pwd
     * 5, 系统各种版本 , sentos .乌班图
     * 6, 软件部署.
     * 7, docker
     * 8, mysql
     * 9, 各种云后台,阿里云, 亚马逊云,腾讯云.
     * @return
     */

    public Retrofit getOkHttpCallAdapterRetrofit(){
        if(noCallAdapterRetrofit == null){
            noCallAdapterRetrofit = new Retrofit.Builder()
                    .baseUrl(Cons.BASE_URL)
                    .client(new OkHttpClient().newBuilder()
                            .addInterceptor(getInterceptor())
                            .addInterceptor(new TokenInterceptor())
                            .build())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return noCallAdapterRetrofit;
    }

    private HttpLoggingInterceptor getInterceptor(){
        return new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);
    }


}   
